package cn.gson.boot.model.service;

import cn.gson.boot.model.dao.*;
import cn.gson.boot.model.mapper.*;
import cn.gson.boot.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ShenHeService {
    @Autowired
    ShenHeMapper shenHeMapper;
    @Autowired
    ShenHeDao shenHeDao;
    @Autowired
    HeTongDao heTongDao;
    @Autowired
    HuanHuoDao huanHuoDao;
    @Autowired
    HuanHuoMapper huanHuoMapper;
    @Autowired
    TuiHuoDao tuiHuoDao;
    @Autowired
    TuiHuoMapper tuiHuoMapper;
    @Autowired
    DinDanDao dinDanDao;
    @Autowired
    FaHuoMapper faHuoMapper;
    @Autowired
    FaHuoDao faHuoDao;
    @Autowired
    JiaoFuJiHuaDao jiaoFuJiHuaDao;
    @Autowired
    HuanHuoXiangQinMapper huanHuoXiangQinMapper;
    @Autowired
    DinDanXiangQingDao dinDanXiangQingDao;
    @Autowired
    QuotationDao quotationDao;
    @Autowired
    PurchaseDao purchaseDao;
    @Autowired
    BusinessDao businessDao;
    @Autowired
    RuKuDao ruKuDao;
    @Autowired
    RuKuXinagqinDao ruKuXinagqinDao;
    @Autowired
    PurchaseDetailsMapper purchaseDetailsMapper;
    @Autowired
    HeTongMapper heTongMapper;
    @Autowired
    SalesDao salesDao;
    @Autowired
    PurchaseReturnDao purchaseReturnDao;
    @Autowired
    RuKuMapper ruKuMapper;
    @Autowired
    ChuKuDao chuKuDao;
    @Autowired
    ChuKuXiangQInDao chuKuXiangQInDao;
    @Autowired
    PurchaseReturnMapper purchaseReturnMapper;
    @Autowired
    SellDao sellDao;
    //查看所有审核
    public List<Shenhe> selectAllShenHe(String tj, String tj2, String tj3) {
        return shenHeMapper.selectAllShenHe(tj,tj2,tj3);
    }

    //查询有审核权限的员工（现在还没写好）
    public List<User> selectSHemp() {
        return shenHeMapper.selectSHemp();
    }

    //确定审核
    public void queDingShenHe(Shenhe shenhe) {
        if(shenhe.getShShouming().equals("合同")){
            heTongDao.shenheHeTong(shenhe.getShZhuangtai(),shenhe.getShTabid());
            if(shenhe.getShZhuangtai().equals("同意")){
                Sales sales = heTongMapper.selectxsjhHeTong(shenhe.getShTabid()).getQuotation().getSales();
                System.out.println(sales);
                Stage stage = new Stage();
                stage.setStageId(5);
                sales.setStage(stage);
                sales.setSalesPossibility("100%");
                salesDao.save(sales);
                System.out.println(sales);
            }
        }else if(shenhe.getShShouming().equals("换货")){
            huanHuoDao.shenheHuanHuo(shenhe.getShZhuangtai(),shenhe.getShTabid());


            List<Huanhuoxiangqing> hhxq = huanHuoXiangQinMapper.ckHuanHuoXiangQin(shenhe.getShTabid());//换货详情
            double ddMoney = 0;
            for(int i=0;i<hhxq.size();i++){
                ddMoney += Double.parseDouble(hhxq.get(i).getHhcpXiaoji().toString());
            }

            //换货单里面的订单
            Dingdan hhDd = huanHuoMapper.selectDingdanByhhId(shenhe.getShTabid());
            //确定换货入库后就生成一个新的订单
            Dingdan newDD = new Dingdan();
            newDD.setkH(hhDd.getkH());//客户id
            newDD.setHetong(hhDd.getHetong());//合同id
            Date date2 = new Date();
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyMMddHHmmss");
            String str2 = sdf2.format(date2);
            Timestamp time2 = new Timestamp(date2.getTime());
            newDD.setDdHao("DD"+str2);//订单号
            newDD.setDdMoney(new BigDecimal(ddMoney));//订单总额
            newDD.setDdTime(time2);//订单时间
            Calendar c = new GregorianCalendar();
            c.setTime(date2);
            c.add(c.DATE,2);//把日期往后增加2天.整数往后推,负数往前移动
            // date2=c.getTime(); //这个时间就是日期往后推一天的结果
            System.err.println("这个时间就是日期往后推一天的结果:"+c.getTime());
            newDD.setDdZuiwanfahuo(new Timestamp(c.getTime().getTime()));//订单最晚发货
            //private String ddFukuanfanshi;//订单付款方式
            newDD.setDdSyz(hhDd.getDdSyz());//订单所有者
            //private String ddZhuangtai;//订单状态
            //private String ddJiekuanfanshi;//订单结款方式
            //private String ddBeizhu;//订单备注
            newDD.setDdYs(1);//判断订单是否移除
            newDD.setProvince(hhDd.getProvince());//省
            newDD.setCity(hhDd.getCity());//市
            newDD.setArea(hhDd.getArea());//区
            newDD.setDdPhone(hhDd.getDdPhone());//电话dd_phone
            newDD.setDdAddress(hhDd.getDdAddress());//详细地址
            newDD.setDdShouhuoren(hhDd.getDdShouhuoren());//收货人
            newDD.setDdZhuangtai("执行中");
            //private String cfgmDdhao;//重复购买的订单号;
            // private BigDecimal ddHkmoney;//订单回款金额
            newDD.setDdIshh(2);//判断订单是否是换货单生成的（2）
            newDD.setDdIshhch(0);
            Dingdan savedd = dinDanDao.save(newDD);
            //新增订单详情
            Dingdanxiangqing ddxq = null;
            for(int j=0;j<hhxq.size();j++){
                ddxq = new Dingdanxiangqing();
                ddxq.setDingdanByDdId(savedd);
                ddxq.setProductt(hhxq.get(j).getProductt());
                ddxq.setDdcpNum(hhxq.get(j).getHhcpNum());
                ddxq.setDdcpPrice(hhxq.get(j).getHhcpPrice());
                ddxq.setDdxqXiaoji(hhxq.get(j).getHhcpXiaoji());
                dinDanXiangQingDao.save(ddxq);
            }

        }else if(shenhe.getShShouming().equals("退货")){
            tuiHuoDao.shenheTuiHuo(shenhe.getShZhuangtai(),shenhe.getShTabid());
        }else if(shenhe.getShShouming().equals("订单")){
            dinDanDao.updateDingDanZt("待发货",shenhe.getShTabid());
        }else if(shenhe.getShShouming().equals("发货")){
           /* //先根据发货id找到订单
            Fahuo fh = faHuoMapper.selecddByFhId(shenhe.getShTabid());
           if(shenhe.getShZhuangtai().equals("同意")){
               dinDanDao.updateDingDanZt("执行中",fh.getDingdanByDdId().getDdId());
               //修改交付计划的状态(根据订单id)
               jiaoFuJiHuaDao.updateJiaoFuJiHuaZt(fh.getDingdanByDdId().getDdId());
           }*/
            faHuoDao.updateFaHuoZt(shenhe.getShZhuangtai(),shenhe.getShTabid());
        }else if(shenhe.getShShouming().equals("交付计划")){
            jiaoFuJiHuaDao.updateJiaoFuJiHuaZt(shenhe.getShZhuangtai(),shenhe.getShTabid());
        }else if(shenhe.getShShouming().equals("报价单")){
            quotationDao.shenheBaoJiaDan(shenhe.getShZhuangtai(),shenhe.getShTabid());
        }else if(shenhe.getShShouming().equals("采购")){
            purchaseDao.shenheCaiGou(shenhe.getShZhuangtai(),shenhe.getShTabid());
            if(shenhe.getShZhuangtai().equals("同意")){
                Ruku ruku = new Ruku();
                ruku.setRkOtherid(shenhe.getShTabid());
                ruku.setRkShuoming("采购入库");
                ruku.setRkYs(0);
                ruku.setRkSqtime(new Timestamp(new Date().getTime()));
                Ruku rukuSave = ruKuDao.save(ruku);
                List<PurchaseDetails> thxq = purchaseDetailsMapper.selectPurchaseDetails(shenhe.getShTabid());//退货详情
                Rukuxiangqin rkxq = null;
                for(int i=0;i<thxq.size();i++){
                    rkxq = new Rukuxiangqin();
                    rkxq.setRuku(rukuSave);
                    rkxq.setProductt(thxq.get(i).getProducttId());
                    rkxq.setRkxqNum(thxq.get(i).getPurdetailsNumber());
                    ruKuXinagqinDao.save(rkxq);
                }

            }
        }else if(shenhe.getShShouming().equals("出差")){
            if(shenhe.getShZhuangtai().equals("同意")){
                businessDao.shenheChuChai(shenhe.getShZhuangtai(),"出差中",shenhe.getShTabid());
            }else {
                businessDao.shenheChuChai(shenhe.getShZhuangtai(),"出差失败",shenhe.getShTabid());
            }
        }else if(shenhe.getShShouming().equals("采购退货")){
            purchaseReturnDao.shenhePurchaseReturn(shenhe.getShZhuangtai(),shenhe.getShTabid());
            //查询入库单表这个采购退货对应的采购单入库了没有，有就新增出库单，没有不新增
            PurchaseReturn purchaseReturn = purchaseReturnDao.findById(shenhe.getShTabid()).get();
            Integer status = ruKuMapper.selectPurchase(purchaseReturn.getPurchaseId().getPurchaseId());
            System.out.println("状态"+status);
            if(status==1){
                Chuku chuku = new Chuku();
                chuku.setCkOtherid(purchaseReturn.getPurReturnId());
                chuku.setCkShouming("采购退货出库");
                chuku.setCkYs(0);
                chuku.setCkSqtime(new Timestamp(new Date().getTime()));
                chuKuDao.save(chuku);
                List<ReturnDetails> thxq = purchaseReturnMapper.selectPurchaseReturnDetails(shenhe.getShTabid());//退货详情
                Chukuxiangqin ckxq = null;
                for(int i=0;i<thxq.size();i++){
                    ckxq = new Chukuxiangqin();
                    ckxq.setChuku(chuku);
                    ckxq.setProductt(thxq.get(i).getProducttId());
                    ckxq.setCkxqNum(thxq.get(i).getReturnDetailsNumber());
                    chuKuXiangQInDao.save(ckxq);
                }

            }
        }else if(shenhe.getShShouming().equals("销售费用")){
            sellDao.shenheBaoJiaDan(shenhe.getShZhuangtai(),shenhe.getShTabid());
        }
        System.out.println("审核人："+shenhe.getEmp().getEmpName());
        shenHeDao.save(shenhe);
    }

    //根据退货id查看退货
    public Tuihuo selectTuiHuoByThId(Integer thId){
        return tuiHuoMapper.selectTuiHuoByThId(thId);
    }
    //根据换货id查看换货
    public Huanhuo selectHuanHuoByThId(Integer hhId){
        return huanHuoMapper.selectHuanHuoByThId(hhId);
    }

    @Autowired
    QuotationDetailsMapper quotationDetailsMapper;
    public List<Quotationdetails> selectQuotationDetailss(Integer quotationId){
        return quotationDetailsMapper.selectQuotationDetailss(quotationId);
    }
    @Autowired
    SellMapper sellMapper;
    //根据销售费用id查找销售费用
    public Sell selectXSFYbysellId(Integer sellId){
        return sellMapper.selectXSFYbysellId(sellId);
    }
}
